package com.woniuxy.oa.web;

import com.woniuxy.oa.entity.Emp;
import com.woniuxy.oa.entity.ResponseVO;
import com.woniuxy.oa.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("emp")
@CrossOrigin("*")
public class EmpController {

    @Autowired
    EmpService empService;

    /**
     * 增加员工Controller
     *
     * @param emp
     * @return
     */
    @GetMapping("add")
    public ResponseVO add(Emp emp) {
        empService.add(emp);
        return ResponseVO.SUC;
    }

    /**
     * 修改员工状态，便于逻辑删除等操作
     *
     * @param id
     * @param status
     * @return
     */
    @GetMapping("updatestatus")
    public ResponseVO updateUserStatusById(String id, String status) {
        empService.updateUserStatusById(id, status);
        return ResponseVO.SUC;
    }

    /**
     * 修改员工个人信息
     *
     * @param emp
     * @return
     */
    @PostMapping("update")
    public ResponseVO updateEmpById(@RequestBody Emp emp) {
        empService.updateEmpById(emp);
        Emp emp1 = empService.getEmpById(emp.getId());
        return ResponseVO.SUC;
    }

    /**
     * 条件查询员工个人信息
     *
     * @param emp
     * @return
     */
    @PostMapping("getAll")
    public ResponseVO<List<Emp>> getAllEmp(Emp emp) {
        System.out.println(emp.getName());
        List<Emp> emps = empService.getAllEmp(emp);
        return new ResponseVO<>("200", "查询成功", emps);
    }

    /**
     * 修改密码
     *
     * @param id
     * @return
     */
    @PostMapping("updatePwd")
    public ResponseVO updatePwd(@RequestBody Map id) {
        Emp emp = empService.getEmpById((String) id.get("id"));
        if (emp.getPassword().equals((String) id.get("password"))) {
            empService.updatePwd((String) id.get("id"), (String) id.get("password"), (String) id.get("newPassword"));
            return ResponseVO.SUC;
        } else {
            return ResponseVO.ERORR;
        }
    }

    /**
     * 通过账号密码登录
     *
     * @param _emp
     * @return
     */
    @PostMapping("login")
    public ResponseVO<Emp> login(@RequestBody Emp _emp) {
        System.out.println("进入login");
        System.out.println(_emp.getName() + "," + _emp.getPassword());
        Emp emp = empService.login(_emp.getName(), _emp.getPassword());
        System.out.println(emp);
        if (emp != null) {
            System.out.println("登陆成功");
            return new ResponseVO<>("200", "ok", emp);
        } else {
            System.out.println("登录失败");
            return new ResponseVO<>("500", "fail");
        }
    }

    /**
     * 测试session是否成功上传
     *
     * @param emp
     * @return
     */
    @GetMapping("test")
    public ResponseVO<Emp> testSession(@SessionAttribute("emps") Emp emp) {
        return new ResponseVO<>("200", "成功", emp);
    }

    /**
     * 退出登录
     *
     * @param status
     * @return
     */
    @GetMapping("logout")
    public ResponseVO loginout(SessionStatus status) {
        status.setComplete();
        return ResponseVO.SUC;
    }

    /**
     * 通过session里边的ID得到当前个人信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/getEmp")
    @CrossOrigin("*")
    public ResponseVO<Emp> getEmp(@RequestBody Map id) {
        System.out.println("到达个人信息后台");

//        Emp emp=(Emp) session.getAttribute("emps");

        Emp emp = empService.getEmpById((String) id.get("id"));
        System.out.println(id.get("id"));
        return new ResponseVO<>("200", "获取个人信息成功", emp);
    }

    /**
     * 查询
     * @param emp
     * @return
     */
    @RequestMapping("/list")
    public ResponseVO list(@RequestBody(required = false) Emp emp) {
        return new ResponseVO<>("200", "查询成功", empService.getPageBean(emp, emp.getPage()));
    }

    /**
     * 逻辑删除
     * @param ids
     * @return
     */
    @GetMapping("del/{ids}")
    public ResponseVO del(@PathVariable String[] ids) {
        for (String id : ids) {
            System.out.println("id: " + id);
        }
        empService.delById(ids);
        return new ResponseVO("200", "删除成功", null);
    }

    /**
     * 查询角色所拥有的权限
     *
     * @param id
     * @return
     */
    @GetMapping("/perm/{id}")
    public ResponseVO permList(@PathVariable String id) {
        return new ResponseVO<>("200", "查询成功", empService.getPerm(id));
    }
    @GetMapping("emps")
    @ResponseBody
    public ResponseVO emps(){
        ResponseVO responseVO=new ResponseVO();
        List<Emp> emps=empService.getEmpAll();
        responseVO.setCode("200");
        responseVO.setData(emps);
        return responseVO;
    }

}

